package com.yuyou.fn.tradingservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.plugins.Page;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wens on 16/4/19.
 */
public class ReturnResult<T> {

    private boolean success;
    private String desc;
    private T data;

    public ReturnResult(boolean success, String desc ) {
        this(success, desc ,null);
    }

    public ReturnResult(boolean success, String desc , T data) {
        this.success = success;
        this.desc = desc;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public static ReturnResult success() {
        return new ReturnResult(true, "OK");
    }

    public static ReturnResult success(String desc) {
        return new ReturnResult(true, desc);
    }

    public static <T> ReturnResult success(Page<T> page) {
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("totalPage", page.getPages());
        data.put("pageIndex", page.getCurrent());
        data.put("total", page.getTotal());
        data.put("list", page.getRecords());
        data.put("extra" , page.getExtra() );
        return new ReturnResult(true, "查询成功" , data);
    }

    public static <T> ReturnResult success(T data) {
        return new ReturnResult(true, "处理成功" , data);
    }

    public static ReturnResult fail(String desc ) {
        return new ReturnResult(false, desc );
    }

    public static ReturnResult fail(String desc , Object data ) {
        return new ReturnResult(false, desc , data );
    }


    public String  toJsonString() {
        return JSONObject.toJSONString(this,true);
    }
}
